import React, { useReducer } from 'react'
// useReducer 用来修改复杂类型数据
// useReducer(function)

let reducer = (state, action) => {
    let NewState = JSON.parse(JSON.stringify(state))
    switch(action.type) {
        case "ADD":
            NewState.age += 1
            return NewState
    }
}

let initState = {
    name: "张三",
    age: 18
}

function Home4() {
    const [state, dispatch] = useReducer(reducer, initState)
    return (
        <div>
            <p>
                姓名: { state.name }
            </p>
            <p>
                年龄: { state.age }
            </p>

            <button onClick = {() => {
                dispatch({ type: "ADD" })
            }}>+++</button>
        </div>
    )
}

export default Home4
